subroutine readgeom(lunit,t_array0,myrank)
  use param
  use data_param,only:glfn
  use data_param,only:geom_exist
  use data_param,only:n_geom_data
  use geom_data_param,only:geom_obj
  use geom_data_param,only:n_geom_obj
  implicit none

  interface
     function reallocGeometricObject(p,n)
       use derived
       type(GeometricObject),pointer,dimension(:) :: &
            reallocGeometricObject,p
       integer,intent(in) :: n
     end function reallocGeometricObject
  end interface

  integer,intent(in) :: lunit,t_array0(8),myrank

  integer :: nunit,count_line,len_str,asci_code
  character(len=len_linebuf) :: eatline,str
  character(len=len_wordbuf),dimension(max_n_word) :: eatword
  character(len=8) :: d_time_str

  integer :: i,in1,in2,pos1,pos2
  integer :: ng,gtype
  
  if (.not.geom_exist) return

  in1=1
  in2=2

  call log_msg1(lunit,"")
  call log_msg1(lunit,"Message from the subourinte (readgeom)")

  call log_msg1(lunit,"")
  write(lunit,'(1x,"Geometric objects")')

  nunit=1

  call drawline(lunit,in1,90)
  write(lunit,'(1x,"idx",1x,&
                4x,"gtype",4x,&
                5x,"x1",5x,&
                5x,"y1",5x,&
                5x,"z1",5x,&
                5x,"x2",5x,&
                5x,"y2",5x,&
                5x,"z2",5x)')
  call drawline(lunit,in1,90)

  open(unit=nunit,file=glfn,status='old',err=103)

  count_line=0
  ng=0
  do
     read(nunit,'(a)',end=100,err=101) eatline
     count_line=count_line+1
     len_str=len_trim(eatline)
     if (len_str.eq.0) cycle
     eatline=adjustl(eatline)
     asci_code=ichar(eatline(1:1))
     if (asci_code.eq.ccode) cycle
     read(eatline(1:len_str),*,end=102) &
          (eatword(i),i=1,max_n_word)
102  continue

     n_geom_data=n_geom_data+1

     select case(eatword(1))
     case("geom")
        ! count the number of geometric objects
        ng=ng+1
        n_geom_obj=ng

        ! Write to the log file
        pos1=0

        pos2=pos1+5
        write(str(pos1+1:pos2),'("[",i3.3,"]")') ng
        pos1=pos2

        ! allocate the memory for the geometric object
        geom_obj=>reallocGeometricObject(geom_obj,ng)

        ! save the index
        geom_obj(ng)%index=ng

        ! save the type of the object
        read(eatword(2),*) gtype
        geom_obj(ng)%geom_type=gtype

        select case(gtype)
        case(1)
           ! point
           read(nunit,*) geom_obj(ng)%x1,&
                         geom_obj(ng)%y1,&
                         geom_obj(ng)%z1

           ! width=9(+4x)
           pos2=pos1+13
           write(str(pos1+1:pos2),'(4x,"point (1)")')
           pos1=pos2

           ! x1,y1,z1
           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%x1
           pos1=pos2

           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%y1
           pos1=pos2

           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%z1
           pos1=pos2
        case(2)
           ! staight line
           read(nunit,*) geom_obj(ng)%x1,&
                         geom_obj(ng)%y1,&
                         geom_obj(ng)%z1
           read(nunit,*) geom_obj(ng)%x2,&
                         geom_obj(ng)%y2,&
                         geom_obj(ng)%z2
           ! width=8(+5x)
           pos2=pos1+13
           write(str(pos1+1:pos2),'(5x,"line (2)")')
           pos1=pos2

           ! x1,y1,z1
           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%x1
           pos1=pos2

           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%y1
           pos1=pos2

           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%z1
           pos1=pos2

           ! x2,y2,z2
           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%x2
           pos1=pos2

           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%y2
           pos1=pos2

           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%z2
           pos1=pos2
        case(3)
           ! x=a,left
           read(nunit,*) geom_obj(ng)%x1
           read(nunit,*) geom_obj(ng)%y1,&
                         geom_obj(ng)%y2,&
                         geom_obj(ng)%z1,&
                         geom_obj(ng)%z2
           ! width=10(+3x)
           pos2=pos1+13
           write(str(pos1+1:pos2),'(3x,"x-left (3)")')
           pos1=pos2

           ! x1
           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%x1
           pos1=pos2
        case(4)
           ! x=a,right
           read(nunit,*) geom_obj(ng)%x1
           read(nunit,*) geom_obj(ng)%y1,&
                         geom_obj(ng)%y2,&
                         geom_obj(ng)%z1,&
                         geom_obj(ng)%z2
           ! width=11(+2x)
           pos2=pos1+13
           write(str(pos1+1:pos2),'(2x,"x-right (4)")')
           pos1=pos2

           ! x1
           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%x1
           pos1=pos2
        case(5)
           ! y=b,left
           read(nunit,*) geom_obj(ng)%y1
           read(nunit,*) geom_obj(ng)%x1,&
                         geom_obj(ng)%x2,&
                         geom_obj(ng)%z1,&
                         geom_obj(ng)%z2
           ! width=10(+3x)
           pos2=pos1+13
           write(str(pos1+1:pos2),'(3x,"y-left (5)")')
           pos1=pos2

           ! x1 (blank)
           pos2=pos1+12
           write(str(pos1+1:pos2),'(12x)')
           pos1=pos2

           ! y1
           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%y1
           pos1=pos2
        case(6)
           ! y=b,right
           read(nunit,*) geom_obj(ng)%y1
           read(nunit,*) geom_obj(ng)%x1,&
                         geom_obj(ng)%x2,&
                         geom_obj(ng)%z1,&
                         geom_obj(ng)%z2           
           ! width=11(+2x)
           pos2=pos1+13
           write(str(pos1+1:pos2),'(2x,"y-right (6)")')
           pos1=pos2

           ! x1 (blank)
           pos2=pos1+12
           write(str(pos1+1:pos2),'(12x)')
           pos1=pos2

           ! y1
           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%y1
           pos1=pos2
        case(7)
           ! z=c,left
           read(nunit,*) geom_obj(ng)%z1
           read(nunit,*) geom_obj(ng)%x1,&
                         geom_obj(ng)%x2,&
                         geom_obj(ng)%y1,&
                         geom_obj(ng)%y2

           ! width=10
           pos2=pos1+13
           write(str(pos1+1:pos2),'(3x,"z-left (7)")')
           pos1=pos2

           ! x1,y1,z1
           pos2=pos1+12
           write(str(pos1+1:pos2),'(12x)')
           pos1=pos2

           pos2=pos1+12
           write(str(pos1+1:pos2),'(12x)')
           pos1=pos2

           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%z1
           pos1=pos2
        case(8)
           ! z=c,right
           read(nunit,*) geom_obj(ng)%z1
           read(nunit,*) geom_obj(ng)%x1,&
                         geom_obj(ng)%x2,&
                         geom_obj(ng)%y1,&
                         geom_obj(ng)%y2

           ! width=11(+2x)
           pos2=pos1+13
           write(str(pos1+1:pos2),'(2x,"z-right (8)")')
           pos1=pos2

           ! x1,y1,z1
           pos2=pos1+12
           write(str(pos1+1:pos2),'(12x)')
           pos1=pos2

           pos2=pos1+12
           write(str(pos1+1:pos2),'(12x)')
           pos1=pos2

           pos2=pos1+12
           write(str(pos1+1:pos2),'(1x,1pe11.5)') &
                geom_obj(ng)%z1
           pos1=pos2
        case default
        end select

     case default
     end select

     call log_msg1_indent(lunit,in1,str(1:pos2))

  end do

103 continue
  call abnormal_stop("readgeom",&
       "Error occurs while the geometric file opens.")

101 continue
  call abnormal_stop("readgeom",&
       "Error occurs during reading the geometric file.")
  
100 continue

  close(nunit)

  call drawline(lunit,in1,90)
  call log_msg1(lunit,"")
  ! Write the message to the terminal and log file
  call print_d_time(t_array0,d_time_str)
  if (myrank.eq.0) &
       write(*,'(a8,":READGEOM DONE.")') d_time_str(1:8)
  write(lunit,'(a8,":READGEOM DONE.")') d_time_str(1:8)

  return

end subroutine readgeom
